<template>
  <div class="header">
    <div class="logo">HMusic后台管理</div>
    <div class="header-right">
      <div class="header-user-con">
        <!-- 用户头像 -->
        <div class="user-avator">
          <img :src="userPic" />
        </div>
        <!-- 用户名下拉菜单 -->
        <el-dropdown class="user-name" trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            {{ userName }}
            <i class="el-icon-caret-bottom"></i>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="loginout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
import bus from "../assets/js/bus";
export default {
  data() {
    return {
      userPic: require("@/assets/img/user.jpg"),
      collapse: true,
    };
  },
  computed:{
      userName(){
          return localStorage.getItem("userName")
      }
  },
  mounted() {},
  methods: {
    // 用户名下拉菜单选择事件
    handleCommand(command) {
      if (command === "loginout") {
        localStorage.removeItem("userName");
        this.$router.replace("/");
      }
    },
    // 侧边栏折叠
    collapseChange() {
      const _this = this;
      _this.collapse = !_this.collapse;
      bus.$emit()("collapse", _this.collapse);
    },
  },
};
</script>

<style scoped>
.header {
  position: relative;
  z-index: 100;
  width: 100%;
  height: 70px;
  display: flex;
  align-items: center;
  font-size: 20px;
  color: #2c3e50;
  background: #ffff;
  box-shadow: 0px 0px 8px 2px rgba(0, 0, 0, 0.3);
}
.collapse-btn {
  float: left;
  padding: 0 21px;
  cursor: pointer;
  line-height: 70px;
}
.header .logo {
  position: relative;
  float: left;
  width: 250px;
  line-height: 70px;
  font-weight: bold;
}
.header-right {
  position: absolute;
  right: 0;
  padding-right: 30px;
}
.header-user-con {
  display: flex;
  align-items: center;
}
.user-name {
  margin-left: 10px;
}
.user-avator img {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.el-dropdown-link {
  color: #b0b3b2;
  cursor: pointer;
}
.el-dropdown-menu__item {
  text-align: center;
}
</style>